/**
 * ChatGroup数据缓存对象.
 */
var ChatGroupStore = function(){
    /**
     * spring board.
     */
    this.on('addItem updateItem', function(item) {
        chatGroupList.cardList.addItem(item);
        sessionList.addSessionItem(item['id']);                
        //chat panel按钮权限改变.
        var perm = item['perm'];
        if(perm){
            var chatPanel = page.contentCardLayout.getItem(item['id']);
            if(chatPanel && chatPanel.chatDialogToolbar){
                var toolbar = chatPanel.chatDialogToolbar.toolbar;
                if(perm.upd){
                    toolbar.showItem('edit');       
                } else {
                    toolbar.hideItem('edit');
                }
            }    
        }
    });
}.extend(Store);

/**
 * 获取  SingleChat 实体对象.
 * @param {String} uid
 */
ChatGroupStore.prototype.getSingleChatByUser = function(uid){
    var singleChats = this.items.filter(function(chatGroupEntity){
        if(chatGroupEntity && chatGroupEntity['type'] === ChatGroupType.SINGLECHAT){
            var participants = chatGroupEntity['extraProps']['participants'];
            if(participants[0]['id'] === uid || participants[1]['id'] === uid){
                return chatGroupEntity;       
            }
        }
    });
    if(singleChats.length){
        return singleChats[0];
    }
};